﻿/*
 * Author: .net follower (http://dotnetfollower.com)
 * Url: http://bdcrequestlib.codeplex.com/
 * Title: SharePoint Bdc Requests
 * Copyright: � 2012 .net follower 
 * Usage: software is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

using System.Data;
using Microsoft.BusinessData.Runtime;

namespace SharePointBdcRequests
{
    public abstract class BdcRequestMulti : BdcRequest
    {
        public virtual IEntityInstance[] FoundEntityInstances
        {
            get { return EntityInstances.ToArray(); }
        }

        /// <summary>
        /// Initializes a new instance of the BdcRequestMulti class
        /// </summary>
        protected BdcRequestMulti(BdcRequestByValueParams bdcRequestParams) : base(bdcRequestParams)
        {
        }

        protected override DataTable GetDataTable()
        {
            if(EntityInstances.Count == 0)
                return new DataTable();
            
            DataTable res = FormattedValues ? EntityInstances[0].EntityAsFormattedDataTable : EntityInstances[0].EntityAsDataTable;

            for (int i = 1; i < EntityInstances.Count; i++)
            {
                DataTable tmpDt = FormattedValues ? EntityInstances[i].EntityAsFormattedDataTable : EntityInstances[i].EntityAsDataTable;
                res.ImportRow(tmpDt.Rows[0]);
            }

            return res;
        }
    }
}
